package com.xhqgov.manage.util;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiDepartmentListRequest;
import com.dingtalk.api.request.OapiSsoGettokenRequest;
import com.dingtalk.api.request.OapiSsoGetuserinfoRequest;
import com.dingtalk.api.request.OapiUserGetRequest;
import com.dingtalk.api.response.OapiDepartmentListResponse;
import com.dingtalk.api.response.OapiSsoGettokenResponse;
import com.dingtalk.api.response.OapiSsoGetuserinfoResponse;
import com.dingtalk.api.response.OapiUserGetResponse;
import com.taobao.api.ApiException;
import com.xhqgov.manage.controller.BaseController;
import com.xhqgov.manage.model.Dept;
import com.xhqgov.manage.vo.AuthHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
public class DDUtil extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(DDUtil.class);
    @Resource
    protected AuthHelper authHelper;
    /**
     * 获取token 用于身份验证
     * @return
     */
    public OapiSsoGettokenResponse gettokenResponse(){
        try{
            DingTalkClient client = new DefaultDingTalkClient(getTokenUrl());
            OapiSsoGettokenRequest request = new OapiSsoGettokenRequest();
            request.setCorpid(getCorpId());
            request.setCorpsecret(getSsoSecret());
            request.setHttpMethod(REQUEST_GET);
            return  client.execute(request);
        }catch (Exception e){
            LOGGER.error("请求钉钉服务器token失败！",e);
            return null;
        }
    }

    /**
     * 判断用户是否是管理员
     * @param code
     * @param accessToken
     * @return
     */
    public OapiSsoGetuserinfoResponse validateAdmin(String code, String accessToken){
        try{
            DingTalkClient client = new DefaultDingTalkClient(getUserInfoUrl());
            OapiSsoGetuserinfoRequest request = new OapiSsoGetuserinfoRequest();
            request.setCode(code);
            request.setHttpMethod(REQUEST_GET);
            return client.execute(request,accessToken);
        }catch (Exception e){
            LOGGER.error("请求钉钉服务器验证管理员失败！",e);
            return null;
        }
    }

    /**
     * 获取用户详细信息
     * @param userId
     * @return
     */
    public OapiUserGetResponse getUserDetail(String userId){

        try {
            DingTalkClient client = new DefaultDingTalkClient(getUserUrl());
            OapiUserGetRequest request = new OapiUserGetRequest();
            request.setUserid(userId);
            request.setHttpMethod(REQUEST_GET);
            OapiUserGetResponse response =  client.execute(request, authHelper.getAccessToken());
            if(response.getErrcode()>0){
                LOGGER.error("获取钉钉用户信息失败！"+response.getErrcode()+":"+response.getErrmsg());
                return null;
            }
            return  response;
        } catch (ApiException e) {
            LOGGER.error("获取钉钉用户信息失败！",e);
            return null;
        }
    }

    /**
     * 从钉钉处获取部门列表
     * @return
     */
    public List<Dept> getDeptListFromDD(){
        try {
            String url = getDeptListUrl();

            DingTalkClient client = new DefaultDingTalkClient(url);
            OapiDepartmentListRequest request = new OapiDepartmentListRequest();
            request.setHttpMethod(REQUEST_GET);
            OapiDepartmentListResponse response = client.execute(request, authHelper.getAccessToken());
            if(response.getErrcode()>0){
                LOGGER.error("获取部门列表失败！"+response.getErrcode()+":"+response.getErrmsg());
                return null;
            }
            List<Dept> deptList = new ArrayList<Dept>();
            for(OapiDepartmentListResponse.Department department:response.getDepartment()){
                Dept dept = new Dept();
                BeanUtils.copyProperties(department,dept);
                deptList.add(dept);
            }
            return deptList;
        } catch (ApiException e) {
            LOGGER.error("获取部门列表失败！",e);
            return null;
        }
    }
}
